package com.fr.data.impl;

import com.fr.base.ArrayUtils;
import com.fr.base.StringUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.TableDataException;
import com.fr.report.core.ParameterHelper;
import com.fr.util.Utils;

public class RecursionTableData extends DBTableData
{
  private String[] markFields;
  private String[] parentmarkFields;
  private String[] treeFields;
  private String fieldName;
  private transient SpecialTreeDataAdapter adapter;
  private String separator = ",";
  private int limitGeneration = -1;

  private TableDataAdapter createAdapter()
  {
    if (this.adapter == null)
    {
      this.adapter = new SpecialTreeDataAdapter(getDatabase(), getNewQuery(), this.markFields, this.parentmarkFields, this.treeFields, this.limitGeneration);
      if (StringUtils.isNotBlank(this.fieldName))
        this.adapter.setDefaultFieldName(this.fieldName);
    }
    return this.adapter;
  }

  public int getLimitGeneration()
  {
    return this.limitGeneration;
  }

  public void setLimitGeneration(int paramInt)
  {
    this.limitGeneration = paramInt;
  }

  public int getColumnCount()
    throws TableDataException
  {
    return createAdapter().getColumnCount();
  }

  public String getColumnName(int paramInt)
    throws TableDataException
  {
    String str = createAdapter().getColumnName(paramInt);
    return str;
  }

  public boolean hasRow(int paramInt)
    throws TableDataException
  {
    return createAdapter().hasRow(paramInt);
  }

  public int getRowCount()
    throws TableDataException
  {
    return createAdapter().getRowCount();
  }

  public Object getValueAt(int paramInt1, int paramInt2)
    throws TableDataException
  {
    return createAdapter().getValueAt(paramInt1, paramInt2);
  }

  public String[] getMarkFields()
  {
    return this.markFields;
  }

  public void setMarkFields(String[] paramArrayOfString)
  {
    this.markFields = paramArrayOfString;
  }

  public String[] getParentmarkFields()
  {
    return this.parentmarkFields;
  }

  public void setParentmarkFields(String[] paramArrayOfString)
  {
    this.parentmarkFields = paramArrayOfString;
  }

  public String[] getTreeFields()
  {
    return this.treeFields;
  }

  public void setTreeFields(String[] paramArrayOfString)
  {
    this.treeFields = paramArrayOfString;
  }

  public String getFieldName()
  {
    return this.fieldName;
  }

  public void setFieldName(String paramString)
  {
    this.fieldName = paramString;
  }

  public void setShare(boolean paramBoolean)
  {
  }

  public boolean isShare()
  {
    return false;
  }

  public void setMaxMemRowCount(int paramInt)
  {
  }

  public int getMaxMemRowCount()
  {
    return -1;
  }

  public int getRealColumnCount()
    throws TableDataException
  {
    return ((SpecialTreeDataAdapter)createAdapter()).getRealColumnCount();
  }

  public String getRealColumnName(int paramInt)
    throws TableDataException
  {
    return ((SpecialTreeDataAdapter)createAdapter()).getRealColumnName(paramInt);
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if ("markFields".equals(str))
        this.markFields = readArray(paramXMLableReader);
      if ("parentmarkFields".equals(str))
        this.parentmarkFields = readArray(paramXMLableReader);
      if ("treeFields".equals(str))
        this.treeFields = readArray(paramXMLableReader);
      if ("fieldName".equals(str))
        this.fieldName = paramXMLableReader.getAttr("defaultName");
      if ("limitGeneration".equals(str))
        this.limitGeneration = Integer.valueOf(paramXMLableReader.getAttr("value")).intValue();
    }
  }

  private String[] readArray(XMLableReader paramXMLableReader)
  {
    String str = paramXMLableReader.getElementValue();
    Object localObject = null;
    if (StringUtils.isNotEmpty(str))
    {
      String[] arrayOfString = Utils.splitString(str, this.separator);
      localObject = arrayOfString;
    }
    return localObject;
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (!(ArrayUtils.isEmpty(this.markFields)))
    {
      paramXMLPrintWriter.startTAG("markFields");
      writeArray(paramXMLPrintWriter, this.markFields);
      paramXMLPrintWriter.end();
    }
    if (!(ArrayUtils.isEmpty(this.parentmarkFields)))
    {
      paramXMLPrintWriter.startTAG("parentmarkFields");
      writeArray(paramXMLPrintWriter, this.parentmarkFields);
      paramXMLPrintWriter.end();
    }
    if (!(ArrayUtils.isEmpty(this.treeFields)))
    {
      paramXMLPrintWriter.startTAG("treeFields");
      writeArray(paramXMLPrintWriter, this.treeFields);
      paramXMLPrintWriter.end();
    }
    if (StringUtils.isNotBlank(this.fieldName))
      paramXMLPrintWriter.startTAG("fieldName").attr("defaultName", this.fieldName).end();
    if (this.limitGeneration > 0)
      paramXMLPrintWriter.startTAG("limitGeneration").attr("value", this.limitGeneration).end();
  }

  private void writeArray(XMLPrintWriter paramXMLPrintWriter, String[] paramArrayOfString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0;
    int j = paramArrayOfString.length;
    while (i < j)
    {
      localStringBuffer.append(paramArrayOfString[i]);
      if (i != j - 1)
        localStringBuffer.append(this.separator);
      ++i;
    }
    paramXMLPrintWriter.textNode(localStringBuffer.toString());
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    RecursionTableData localRecursionTableData = new RecursionTableData();
    localRecursionTableData.setDatabase(getDatabase());
    localRecursionTableData.parameters = ParameterHelper.cloneParameters(getParameters());
    localRecursionTableData.setQuery(getQuery());
    localRecursionTableData.setPageQuerySql(getPageQuerySql());
    localRecursionTableData.setFieldName(getFieldName());
    localRecursionTableData.setMarkFields(this.markFields);
    localRecursionTableData.setParentmarkFields(this.parentmarkFields);
    localRecursionTableData.setTreeFields(this.treeFields);
    localRecursionTableData.separator = this.separator;
    localRecursionTableData.limitGeneration = this.limitGeneration;
    return localRecursionTableData;
  }

  public void release()
    throws Exception
  {
    if (this.adapter != null)
    {
      this.adapter.unRegister(this);
      this.adapter.tryDestroy();
      this.adapter = null;
    }
  }
}